
#ifndef __HTTPSERVICE__
#define __HTTPSERVICE__

#include <iostream>
#include "TcpSocket.hpp"
#include <unistd.h>
#include <signal.h>
#include <functional>

using func_t = std::function<void(int)>;

class Httpserver
{
    int listen_socket_ = -1 ;
    int port_;
    MySocket mysocket;
    func_t func_;

public:
    Httpserver(int port, func_t func): port_(port), func_(func)
    {
        listen_socket_ = mysocket._socket_set();
        mysocket.bind_set(listen_socket_, port_);
        mysocket.listen_run(listen_socket_);
    }

    void Stat()
    {
        signal(SIGCHLD, SIG_IGN); //忽略子进程返回，子进程结束自动回收
        for (;;)
        {
            struct sockaddr_in send_;
            uint16_t send_port; 
            int actual_socket = mysocket.accept_run(listen_socket_, &send_, &send_port);
            std::cout<< 1 << std::endl;
            // 就是看安排任务了
            pid_t id = fork();
            if (id == 0)
            {
                close(listen_socket_);
                func_(actual_socket);
                exit(0);
            }
            close(actual_socket);
        }
    }

    ~Httpserver()
    {
        if (listen_socket_ > 0) close(listen_socket_);
    }
};










#endif


